Instructions

Answer the following questions and complete the exercises in RMarkdown. Please embed all of your code and push your final work to your repository. Your final lab report should be organized, clean, and run free from errors. Remember, you must remove the # for the included code chunks to run. Be sure to add your name to the author header above. For any included plots, make sure they are clearly labeled. You are free to use any plot type that you feel best communicates the results of your analysis.

Make sure to use the formatting conventions of RMarkdown to make your report neat and clean!

Load the libraries

library(tidyverse)
library(janitor)
library(ggmap)
library(leaflet)

API Key

register_stadiamaps("f9de653c-55a3-4f5b-9672-63291557e012", write = FALSE)

Load the Data

For this homework, we will use the shark attack data to visualize where the attacks occurred. The data are from: State of California- Shark Incident Database.

sharks <- read_csv("data/SharkIncidents_1950_2022_220302.csv") %>% 
  clean_names() %>% 
  filter(longitude !="NA" & latitude !="NA") %>% # pulling out NA locations
  mutate(longitude = as.numeric(longitude)) # converting longitude to numeric
  1. Use the range of the latitude and longitude to build an appropriate bounding box for your map.
sharks %>% 
  select(latitude, longitude) %>% 
  summary()
##     latitude       longitude     
##  Min.   :32.59   Min.   :-124.7  
##  1st Qu.:34.04   1st Qu.:-123.1  
##  Median :36.70   Median :-122.0  
##  Mean   :36.36   Mean   :-121.4  
##  3rd Qu.:38.18   3rd Qu.:-119.6  
##  Max.   :41.56   Max.   :-117.1
lat <- c(32.59, 41.56)
long <- c(-124.7, -117.1)
bbox <- make_bbox(long, lat, f=0.03)
  1. Load a map from stamen in a terrain style projection and display the map.
mapshark <- get_stadiamap(bbox, maptype="stamen_terrain", zoom=7)
## ℹ © Stadia Maps © Stamen Design © OpenMapTiles © OpenStreetMap contributors.
ggmap(mapshark)

  1. Build a map that overlays the recorded observations of shark attacks in California.
ggmap(mapshark)+
  geom_point(data=sharks, aes(longitude, latitude), size=0.25, color="orangered", alpha=0.8)+
  labs(title="California Shark Attacks", x="Longitude", y="Latitude")

  1. Build a map that only shows the fatalities.
ggmap(mapshark)+
  geom_point(data=sharks %>% filter(injury=="fatal"), aes(longitude, latitude), size=0.25, color="red3", alpha=0.8)+
  labs(title="California Shark Fatalities", x="Longitude", y="Latitude")

  1. Build the same map (fatalities only), but this time make it interactive using leaflet. Also, color the points red.
long_min <- -124.7
long_max <- -117.1
lat_min <- 32.59
lat_max <- 41.56
leaflet(sharks %>% filter(injury=="fatal")) %>% 
  addProviderTiles(providers$Stadia.StamenTerrain) %>% 
  addCircleMarkers(
    lng=~longitude,
    lat=~latitude,
    radius=1.5,
    stroke=F,
    fillOpacity=0.9,
    color="red"
  ) %>% 
  addScaleBar(position="bottomleft") %>% 
  fitBounds(lng1=long_min, lat1=lat_min,
            lng2=long_max, lat2=lat_max)
  1. Which county has the highest number of recorded shark attacks? Make a map that only shows the attacks in this county. (hint: it may help improve the map if you remove duplicate locations, i.e. multiple attacks at the same location.)

San Diego has the highest number of recorded shark attacks.

sharks %>% 
  group_by(county) %>% 
  summarize(n=n()) %>% 
  arrange(desc(n))
## # A tibble: 21 × 2
##    county              n
##    <chr>           <int>
##  1 San Diego          24
##  2 San Mateo          19
##  3 Santa Barbara      19
##  4 Humboldt           18
##  5 Marin              16
##  6 Monterey           16
##  7 Santa Cruz         15
##  8 Sonoma             15
##  9 San Luis Obispo    14
## 10 Los Angeles         9
## # ℹ 11 more rows
leaflet(sharks %>% filter(county=="San Diego") %>% distinct(location, .keep_all=T)) %>% 
  addProviderTiles(providers$Stadia.StamenTerrain) %>% 
  addCircleMarkers(
    lng=~longitude,
    lat=~latitude,
    radius=1.5,
    stroke=F,
    fillOpacity=0.9,
    color="darkorange"
  ) %>% 
  addScaleBar(position="bottomleft") %>% 
  fitBounds(lng1=long_min, lat1=lat_min,
            lng2=long_max, lat2=lat_max)

Knit and Upload

Please knit your work as an .html file and upload to Canvas. Homework is due before the start of the next lab. No late work is accepted. Make sure to use the formatting conventions of RMarkdown to make your report neat and clean!